an exception is occurred while play an audio in blackberry - blackberry

try {
Player p = javax.microedition.media.Manager.createPlayer("abc.mp3");
p.realize();
VolumeControl volume = (VolumeControl)p.getControl("VolumeControl");
volume.setLevel(30);
p.prefetch();
p.start();
} catch(MediaException me) {
Dialog.alert(me.toString());
} catch(IOException ioe) {
Dialog.alert(ioe.toString());
}
I used above code to play an audio in blackberry. But it gives an exception error as mentioned in below.
javax.microedition.media.MediaException

If you look at the BlackBerry API docs, you see that for the version of createPlayer() that you use:
MediaException - Thrown if a Player cannot be created for the given locator.
It also says:
locator - A locator string in URI syntax that describes the media content.
Your locator ("abc.mp3") doesn't look like it's in URI syntax. You could try changing that, to something like "file:///SDCard/some-folder/abc.mp3".
Or, if the mp3 file is an app resource, I usually use this code:
try {
java.io.InputStream is = getClass().getResourceAsStream("/sounds/abc.mp3");
javax.microedition.media.Player p =
javax.microedition.media.Manager.createPlayer(is, "audio/mpeg");
p.realize();
// get volume control for player and set volume to max
VolumeControl vc = (VolumeControl) p.getControl("VolumeControl");
if (vc != null) {
vc.setLevel(100);
}
// the player can start with the smallest latency
p.prefetch();
// non-blocking start
p.start();
} catch (javax.microedition.media.MediaException me) {
// do something?
} catch (java.io.IOException ioe) {
// do something?
} catch (java.lang.NullPointerException npe) {
// this happened on Tours without mp3 bugfix
}

Related

Gluon iOS audio Player

I've been following various answers to various questions on the subject and I've come to a result and some code which looks like it works. I'm getting stuck with the NSURL part of it. I've got 2 mp3 tracks in the assets folder of my iOS gluon project. I've made the IOSAudioService Class to handle the playback. and I'm passing an argument from the play button in the view to the Play() method. Everything other than the actual file is registering as working. I'm getting an NSError, which from looking at the code is a nil value, so either the argument isn't passing correctly or it can't find the file. Code below.
public AVAudioPlayer backgroundMusic;
private double currentPosition;
NSURL songURL = null;
#Override
public void Play(String filename){
songURL = new NSURL(filename);
try {
if (backgroundMusic != null) {
Resume();
}
else {
//Start the audio at the beginning.
currentPosition = 0;
backgroundMusic = new AVAudioPlayer(songURL);
//create the mendia player and assign it to the audio
backgroundMusic.prepareToPlay();
backgroundMusic.play();}
//catch the audio error
} catch(NSErrorException e) {
System.out.println("error: " + e);
}
}
#Override
public void Stop() {
backgroundMusic.stop();
backgroundMusic = null;
}
#Override
public void Pause() {
currentPosition = backgroundMusic.getCurrentTime();
backgroundMusic.pause();
}
#Override
public void Resume() {
backgroundMusic.setCurrentTime(currentPosition);
backgroundMusic.play();
}
try {
services = (AudioService) Class.forName("com.gluonhq.charm.down.plugins.ios.IOSAudioService").newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.out.println("Error " + ex);
}
I'm getting the error at the catch block for NSExceptionError e.
if (services != null) {
final HBox hBox = new HBox(10,
MaterialDesignIcon.PLAY_ARROW.button(e -> services.Play("/audio.mp3")),
MaterialDesignIcon.PAUSE.button(e -> {
if (!pause) {
services.Pause();
pause = true;
} else {
services.Resume();
pause = false;
}
}),
MaterialDesignIcon.STOP.button(e -> services.Stop()));
//set the HBox alignment
hBox.setAlignment(Pos.CENTER);
hBox.getStyleClass().add("hbox");
//create and set up a vbox to include the image, audio controls and the text then set the alignment
final VBox vBox = new VBox(5, Image(), hBox, text1);
vBox.setAlignment(Pos.CENTER);
setCenter(new StackPane(vBox));
} else {
//start an error if service is null
setCenter(new StackPane(new Label("Only for Android")));
}
Services.get(LifecycleService.class).ifPresent(s -> s.addListener(LifecycleEvent.PAUSE, () -> services.Stop()));
}
I've also follow the advice on creating the service factory class and the interface from Audio performance with Javafx for Android (MediaPlayer and NativeAudioService) taking out the add audio element and I'm intending to do this on a view by view basis if possible.
Problem solved after must fiddling and looking in the Javadocs.Solved by adding/replacing some code with the below.
songURL = new NSURL("file:///" + NSBundle.getMainBundle().findResourcePath(filename, "mp3"));
try {
songURL.checkResourceIsReachable();}
catch(NSErrorException d) {
System.out.println("Song not found!" + d);
}

FFmpegInterop stream to youtube - Windows10 IoT

I need to get data from rtsp link like code below and stream to youtube rtmp like: rtmp://a.rtmp.youtube.com/live2/e1j9-ag98-vg5p-avhc
but I dont know how to do next with MediaStreamSource
My application will be deploy to RapsberryPi3 - Windows10 IoT ARM
Please help me
try
{
string uri = "rtsp://27.74.XXX.XXX:55/ufirststream";
// Read toggle switches states and use them to setup FFmpeg MSS
bool forceDecodeAudio = toggleSwitchAudioDecode.IsOn;
bool forceDecodeVideo = toggleSwitchVideoDecode.IsOn;
// Set FFmpeg specific options. List of options can be found in https://www.ffmpeg.org/ffmpeg-protocols.html
PropertySet options = new PropertySet();
// Below are some sample options that you can set to configure RTSP streaming
options.Add("rtsp_flags", "prefer_tcp");
options.Add("allowed_media_types", "video");
options.Add("stimeout", 100000 * 5);
options.Add("reorder_queue_size", 1);
options.Add("packet-buffering", 0);
options.Add("fflags", "nobuffer");
options.Add("probesize", 32);
// Instantiate FFmpegInteropMSS using the URI
mediaElement.Stop();
FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromUri(uri, forceDecodeAudio, forceDecodeVideo, options);
if (FFmpegMSS != null)
{
MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();
if (mss != null)
{
// Pass MediaStreamSource to Media Element
mediaElement.SetMediaStreamSource(mss);
// Close control panel after opening media
Splitter.IsPaneOpen = false;
}
else
{
DisplayErrorMessage("Cannot open media");
}
}
else
{
DisplayErrorMessage("Cannot open media");
}
}
catch (Exception ex)
{
DisplayErrorMessage(ex.Message);
}

AVAudioplayer initialize component throws a null exception

string mediafile;
mediafile=Constants.AudioLink;
NSUrl url1=new NSUrl(mediafile);
var audioplayer=AVAudioPlayer.FromUrl(url1);
URL is getting link correctly but audio player getting value null. It throws an exception below:
Could not initialize an instance of the type 'AVFoundation.AVAudioPlayer': the native 'initWithContentsOfURL:error:' method returned nil.
It is possible to ignore this condition by setting : MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false
What can i do, Please give me the solution.
These is a really quick example of loading/playing an .mp3 file that has a build type of BundledResource (I dropped into the Resources folder of the project) and thus is placed in the app's root directory).
I do a quick check via AudioToolbox.AudioSource.Open to make sure the file exists, can be read, and is a valid media file type so I know that AVAudioPlayer will not throw a fatal error trying to load it.
AVAudioPlayer player; // Class level object ref
partial void playButtonTouch (UIButton sender)
{
if (player != null && player.Playing)
player.Stop ();
else {
var mp3File = "WildTurkeysEN-US.mp3";
var mp3URL = new NSUrl (mp3File);
Console.WriteLine (mp3URL.AbsoluteUrl);
var mp3 = AudioToolbox.AudioSource.Open (mp3URL, AudioFilePermission.Read, AudioFileType.MP3);
if (mp3 != null) {
Console.WriteLine (mp3.EstimatedDuration);
player = AVAudioPlayer.FromUrl (mp3URL);
player.Play ();
} else {
Console.WriteLine ( "File could not be loaded: {0}", mp3URL.FilePathUrl );
}
}
}

Deferring persistence as device is being used in BlackBerry when listening file change

I tried to listen file change event in BlackBerry base on FileExplorer example, but whenever I added or deleted file, it always showed "Deferring persistence as device is being used" and I can't catch anything .Here is my code:
public class FileChangeListenner implements FileSystemJournalListener{
private long _lastUSN; // = 0;
public void fileJournalChanged() {
long nextUSN = FileSystemJournal.getNextUSN();
String msg = null;
for (long lookUSN = nextUSN - 1; lookUSN >= _lastUSN && msg == null; --lookUSN)
{
FileSystemJournalEntry entry = FileSystemJournal.getEntry(lookUSN);
// We didn't find an entry
if (entry == null)
{
break;
}
// Check if this entry was added or deleted
String path = entry.getPath();
if (path != null)
{
switch (entry.getEvent())
{
case FileSystemJournalEntry.FILE_ADDED:
msg = "File was added.";
break;
case FileSystemJournalEntry.FILE_DELETED:
msg = "File was deleted.";
break;
}
}
}
_lastUSN = nextUSN;
if ( msg != null )
{
System.out.println(msg);
}
}
}
Here is the caller:
Thread t = new Thread(new Runnable() {
public void run() {
new FileChangeListenner();
try {
Thread.sleep(5000);
createFile();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t.start();
Create file method worked fine:
private void createFile() {
try {
FileConnection fc = (FileConnection) Connector
.open("file:///SDCard/newfile.txt");
// If no exception is thrown, then the URI is valid, but the file
// may or may not exist.
if (!fc.exists()) {
fc.create(); // create the file if it doesn't exist
}
OutputStream outStream = fc.openOutputStream();
outStream.write("test content".getBytes());
outStream.close();
fc.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
and output:
0:00:44.475: Deferring persistence as device is being used.
0:00:46.475: AG,+CPT
0:00:46.477: AG,-CPT
0:00:54.476: VM:+GC(f)w=11
0:00:54.551: VM:-GCt=9,b=1,r=0,g=f,w=11,m=0
0:00:54.553: VM:QUOT t=1
0:00:54.554: VM:+CR
0:00:54.596: VM:-CR t=5
0:00:55.476: AM: Exit net_rim_bb_datatags(291)
0:00:55.478: Process net_rim_bb_datatags(291) cleanup started
0:00:55.479: VM:EVTOv=7680,w=20
0:00:55.480: Process net_rim_bb_datatags(291) cleanup done
0:00:55.481: 06/25 03:40:41.165 BBM FutureTask Execute: net.rim.device.apps.internal.qm.bbm.platform.BBMPlatformManagerImpl$3#d1e1ec79
0:00:55.487: 06/25 03:40:41.171 BBM FutureTask Finish : net.rim.device.apps.internal.qm.bbm.platform.BBMPlatformManagerImpl$3#d1e1ec79
I also tried to remove the thread or create or delete file in simulator 's sdcard directly but it doesn't help. Please tell me where is my problem. Thanks
You instantiate the FileChangeListenner, but you never register it, and also don't keep it as a variable anywhere. You probably need to add this call
FileChangeListenner listener = new FileChangeListenner();
UiApplication.getUiApplication().addFileSystemJournalListener(listener);
You also might need to keep a reference (listener) around for as long as you want to receive events. But maybe not (the addFileSystemJournalListener() call might do that). But, you at least need that call to addFileSystemJournalListener(), or you'll never get fileJournalChanged() called back.

Capturing with vlcj only gives a corrupt video file

I am using vlcj to capture the screen in my Java program. Therefore I use the following code:
public static void main(final String[] args) {
NativeLibrary.addSearchPath("vlc", "/Applications/VLC.app/Contents/MacOS/lib/");
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new CaptureTest().start("screen://");
}
});
}
public CaptureTest() {
factory = new MediaPlayerFactory();
mediaPlayer = (HeadlessMediaPlayer) factory.newMediaPlayer();
}
private void start(String mrl) {
File dir = new File(System.getProperty("user.home"), "Videos");
dir.mkdirs();
DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
String fileName = dir.getAbsolutePath() + "/Capture-" + df.format(new Date()) + ".mp4";
String[] options = {
":sout=#transcode{vcodec=h264,acodec=mp4a}:std{mux=mp4,access=file,dst=" + fileName + "}", ":input-slave=screen://"
};
mediaPlayer.playMedia(mrl, options);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mediaPlayer.stop();
mediaPlayer.release();
}
The problem is that the video output file is only 4KB and you can't play it. Can anyone help me? I am on Mac OS 10.6.8 and I use VLC 1.1.12 and vlcj 1.1.5
Here is the interesting part of your code...
mediaPlayer.playMedia(mrl, options);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mediaPlayer.stop();
mediaPlayer.release();
Why did you play the media, slept for 5 seconds and stopped immediately after that? Maybe, this is why you get a very small file size during the screen recording. From what I notice, the transcode is not so fast enough, so the file is not increasing in size immediately (maybe due to buffering takes place during transcoding part, I guess...)
The best is to create a button each for the play/record action and for the stop action.
You might check your sout chain. I also had problem with video file generation. The working sout chain for me is :
:sout=#transcode{vcodec=mp2v, vb=4096,scale=1, acodec=mpga, ab=128, channels=2, samplerate=44100}
:duplicate{dst=file{dst="+ fileName+"}, dst=display, select=noaudio}

Resources