Skip to content

Commit baca426

Browse files
committed
complete image support
1 parent 472dff8 commit baca426

File tree

8 files changed

+16
-31
lines changed

8 files changed

+16
-31
lines changed

build.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ fn main() {
1818
"AVFoundation",
1919
"-framework",
2020
"AppKit",
21-
"-framework",
22-
"Cocoa",
2321
"-o",
2422
&lib_path,
2523
swift_file,

examples/RustAudio.png

869 KB
Loading

examples/basic.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
use system_media::MediaSession;
2-
use std::fs;
31
use std::thread;
42
use std::time::Duration;
3+
use system_media::MediaSession;
54

65
fn main() {
76
println!("Now Playing session started");
87
println!("Keeping process alive... Press Ctrl+C to exit");
98
let mut session = MediaSession::new();
109

1110
let manifest_dir = env!("CARGO_MANIFEST_DIR");
12-
let path = format!("{}/examples/bug.png", manifest_dir);
11+
let path = format!("{}/examples/RustAudio.png", manifest_dir);
1312

1413
session.set_playback_rate(1.0);
1514
session.set_playback_duration(300.0);
1615
session.set_elapsed_duration(100.0);
1716

18-
session.set_title("Help ME!");
17+
session.set_title("Example Title");
18+
session.set_artist("RustAudio");
19+
session.set_album("Audio and Video");
1920

20-
session.set_image(&path);
21+
session.set_image(&path).unwrap();
2122

2223
session.start();
2324
}

examples/bug.png

-1.58 MB
Binary file not shown.

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ mod macos;
33
#[cfg(not(any(target_os = "macos")))]
44
mod null;
55

6-
use std::sync::{Arc, Mutex};
76
use std::error::Error;
7+
use std::sync::{Arc, Mutex};
88

99
pub trait MediaBackend {
1010
fn set_title(&self, title: &str);
1111
fn set_artist(&self, artist: &str);
1212
fn set_album(&self, album: &str);
1313
fn set_genre(&self, genre: &str);
14-
fn set_image(&self, path: &str);
14+
fn set_image(&self, path: &str) -> Result<(), Box<dyn Error>>;
1515
fn set_media_type(&self, media_type: MediaType);
1616
fn set_playback_duration(&self, duration: f64);
1717
fn set_elapsed_duration(&self, duration: f64);
@@ -108,7 +108,7 @@ impl MediaSession {
108108

109109
pub fn set_image(&mut self, path: &str) -> Result<(), Box<dyn Error>> {
110110
Arc::clone(&self.metadata).lock().unwrap().image_path = path.to_string();
111-
self.backend.set_image(path);
111+
self.backend.set_image(path)?;
112112
Ok(())
113113
}
114114

src/linux/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/macos/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{MediaBackend, MediaType};
22
use std::ffi::CString;
3-
use std::thread;
3+
use std::error::Error;
44

55
// Swift functions called by Rust
66
unsafe extern "C" {
@@ -64,12 +64,13 @@ impl MediaBackend for NowPlayingBackend {
6464
swift_set_metadata_genre(str_to_raw(genre));
6565
}
6666
}
67-
68-
fn set_image(&self, path: &str) {
69-
let data = std::fs::read(path).unwrap();
67+
68+
fn set_image(&self, path: &str) -> Result<(), Box<dyn Error>> {
69+
let data = std::fs::read(path)?;
7070
unsafe {
7171
swift_set_metadata_image(data.as_ptr(), data.len());
7272
}
73+
Ok(())
7374
}
7475

7576
fn set_media_type(&self, media_type: MediaType) {

src/macos/nowplaying.swift

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Foundation
22
import MediaPlayer
33
import AVFoundation
44
import AppKit
5-
import Cocoa
65

76
@_silgen_name("rust_resume_playback_command")
87
public func rustStartPlaybackCommand()
@@ -56,21 +55,6 @@ public func setMetadataGenre(genre : UnsafePointer<CChar>) {
5655
}
5756
}
5857

59-
func resize(image: NSImage, to newSize: NSSize) -> NSImage {
60-
let resizedImage = NSImage(size: newSize)
61-
resizedImage.lockFocus()
62-
defer { resizedImage.unlockFocus() }
63-
64-
image.draw(
65-
in: NSRect(origin: .zero, size: newSize),
66-
from: NSRect(origin: .zero, size: image.size),
67-
operation: .copy,
68-
fraction: 1.0
69-
)
70-
71-
return resizedImage
72-
}
73-
7458
@_cdecl("swift_set_metadata_image")
7559
public func setMetadataImage(bytes: UnsafePointer<UInt8>, length: Int) {
7660
DispatchQueue.main.async {
@@ -84,7 +68,7 @@ public func setMetadataImage(bytes: UnsafePointer<UInt8>, length: Int) {
8468
newImage.unlockFocus()
8569
return newImage
8670
}
87-
71+
8872
var info = MPNowPlayingInfoCenter.default().nowPlayingInfo ?? [:]
8973
info[MPMediaItemPropertyArtwork] = artwork
9074
MPNowPlayingInfoCenter.default().nowPlayingInfo = info

0 commit comments

Comments
 (0)