Supported music files by BASS audio library - bass

Is there any way to know if a file extension is supported by Bass audio library (such as WAV/AIFF/MP3/MP2/MP1/OGG )? I would like to write a function like :
Public void Play(string File)
{
if(IsSupportedFile(file)
{
// Add the appropriate code to play the file
}
}
Public bool isSupported(string File)
{
// Should return true if the file extension is supported by bass audio and add ons
}

Give this a try:
Public bool isSupported(string File)
{
return Un4seen.Bass.Utils.BASSAddOnIsFileSupported(Nothing, file);
}

Related

How to play audio in xamarin forms?

I am trying to play an audio file which is stored locally. I am using Xam.Plugin.SimpleAudioPlayer for playing audio. For UWP and Android, added the files in the Assets folder with the Build Action set to Content and Android Asset respectively.
My Code:
private void PlayAudio()
{
try
{
var stream = GetStreamFromFile("audio.mp3");
var audio = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
audio.Load(stream);
audio.Play();
}
catch (Exception e)
{
Debug.WriteLine("exception:>>" + e);
}
}
Stream GetStreamFromFile(string filename)
{
var assembly = typeof(App).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("AudioPlay." + filename);
return stream;
}
But getting an exception in android and UWP, didn't check in IOS.
Android Exception:
[0:] exception:>>System.NullReferenceException: Object reference not set to an instance of an object.
at Plugin.SimpleAudioPlayer.SimpleAudioPlayerImplementation.Load (System.IO.Stream audioStream) [0x00050] in C:\dev\open\Xamarin-Plugins\SimpleAudioPlayer\SimpleAudioPlayer\Plugin.SimpleAudioPlayer.Android\SimpleAudioPlayerImplementation.cs:100
at AudioPlay.MainPage.PlayAudio () [0x00014] in F:\AudioPlay\AudioPlay\AudioPlay\MainPage.xaml.cs:63
I am using .mp3 file. Am I missing something in this implementation? Please help me to fix this issue?
You just need to directly put the mp3 file in share project . And call the method
var stream = GetStreamFromFile("xxx.mp3");
var audio = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
audio.Load(stream );
audio.Play();
Stream GetStreamFromFile(string filename)
{
var assembly = typeof(App).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("yourprojectname." + filename);
return stream;
}
Update
I check your demo , and if you want to play mp3 file in your project. You need to set the build action of the mp3 as Embedded resource
Right click the mp3 file -> Property
try this For xamarin.android
var u = Android.Net.Uri.Parse("filepath");
MediaPlayer _player = MediaPlayer.Create(this, u);
_player.Start();
or
use XamarinMediaManager plugin
https://github.com/martijn00/XamarinMediaManager
The accepted answer does not work when attempting to load a resource from a project that is not the main Xamarin Forms project.
The solution is to use Android.AssetManager.Open() to get a Stream (I'm sure iOS has something similar). You'll need to use DependencyService to access this outside the Android project.
The MP3 files would be put in the Assets folder of the Android project. The audio files need to have Build Action: "Android Asset", not "Embedded Resource" as above.
Example:
Android Project has Assets/Audio/click.mp3
AndroidProject/AndroidAssetManager.cs
[assembly: Dependency (typeof (AndroidAssetManager))]
namespace App.Droid
{
public interface IAssetManager
{
Stream LoadAsset(string assetName);
...
}
public class AndroidAssetManager : IAssetManager
{
private readonly AssetManager _assets;
public AndroidAssetManager(AssetManager assets)
{
_assets = assets;
}
public Stream LoadAsset(string assetName)
{
return _assets.Open(assetName);
}
...
}
}
Shared/AudioPlayer.cs:
class AudioPlayer : IAudioPlayer
{
private IAssetManager AssetManager => DependencyService.Get<IAssetManager>();
private ISimpleAudioPlayer _clickPlayer;
public AudioPlayer()
{
_clickPlayer = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
_clickPlayer.Load(AssetManager.Load("Audio/click.mp3"));
}
public void PlayClick()
{
_clickPlayer.Play();
}
}

How to add 2 sounds and play them via script?

I am made a door script and it works fine but now i want to add different sounds when the door opens and when it closes. I added an Audio Source at the door and added the doorOpen sound. How can i add a doorClose sound too and make it play via the script?
Audio Source
if (open) {
GetComponent<AudioSource>().Play ();
} else {
GetComponent<AudioSource>().Play ();
}
Check Audio & Sound Tutorial. Here is sample code:
using UnityEngine;
using System.Collections;
namespace Completed
{
public class SoundManager : MonoBehaviour
{
public AudioSource efxSource; //Drag a reference to the audio source which will play the sound effects.
public AudioSource musicSource; //Drag a reference to the audio source which will play the music.
public static SoundManager instance = null; //Allows other scripts to call functions from SoundManager.
public float lowPitchRange = .95f; //The lowest a sound effect will be randomly pitched.
public float highPitchRange = 1.05f; //The highest a sound effect will be randomly pitched.
void Awake ()
{
//Check if there is already an instance of SoundManager
if (instance == null)
//if not, set it to this.
instance = this;
//If instance already exists:
else if (instance != this)
//Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.
Destroy (gameObject);
//Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene.
DontDestroyOnLoad (gameObject);
}
//Used to play single sound clips.
public void PlaySingle(AudioClip clip)
{
//Set the clip of our efxSource audio source to the clip passed in as a parameter.
efxSource.clip = clip;
//Play the clip.
efxSource.Play ();
}
//RandomizeSfx chooses randomly between various audio clips and slightly changes their pitch.
public void RandomizeSfx (params AudioClip[] clips)
{
//Generate a random number between 0 and the length of our array of clips passed in.
int randomIndex = Random.Range(0, clips.Length);
Keep reference to both the Audio files.
Then,
if (open) {
GetComponent<AudioSource> ().clip = _OpenClip;
GetComponent<AudioSource> ().Play ();
} else {
GetComponent<AudioSource> ().clip = _CloseClip;
GetComponent<AudioSource> ().Play ();
}

Vaadin audio element doesn't work when I use it to play .m4a sound files (Vaadin 7.3.2)

I use this to display html5 audio in one of my Vaadin application. When I simply requst the data in browser, and save the file, it can be played - but it always fails to do so, when I try it in Vaadin.
Can you point out, what am I doing wrong?
public class AudioArea extends Audio {
public AudioArea(final int soundId) {
super();
StreamResource resource = new StreamResource(
new StreamResource.StreamSource() {
public InputStream getStream() {
byte[] data = MyVaadinUI.request.getBinaryData(
soundId, BinaryDataSource.TYPE_SOUND, 0, 0);
if (data == null) {
return null;
}
return new ByteArrayInputStream(data);
}
}, "");
setSource(resource);
markAsDirty();
}
}
Are you sure that your browser supports the format? If you try it with plain html can you play it?
If the browser supports the format, then I suggest this:
Audio audio = new Audio("MyAudio");
audio.setSource(new ThemeResource("audio/myAudio.m4a"));
myLayout.addComponent(audio);
And you could put the file under your theme folder (or you can use FileResource instead).
EDIT
Maybe the missing MIME Type is the problem:
StreamResource resource = new StreamResource(
new StreamResource.StreamSource() {
public InputStream getStream() {
byte[] data = MyVaadinUI.request.getBinaryData(
soundId, BinaryDataSource.TYPE_SOUND, 0, 0);
if (data == null) {
return null;
}
return new ByteArrayInputStream(data);
}
}, "") {
#Override
public String getMIMEType() {
return "audio/mp4";
}
};

Reproducing audio selected with jfilechooser in netbeans

Ok, here's the code:
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import sun.audio.*;
public class Sound {
public static void main ( String Args[]){
JFileChooser openf =new JFileChooser();
openf.showOpenDialog(null);
File fl= openf.getSelectedFile();
String sound = fl.getAbsolutePath();
JOptionPane.showMessageDialog(null, sound);
InputStream in;
try{
in = new FileInputStream(sound);
AudioStream audio = new AudioStream(in);
AudioPlayer.player.start(audio);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
}
}
Im working this application to allow the selection of audio files (through the jfilechooser) such as: mp3, wma or wav for its reproduction.
The exception I keep having is the following: 'java.io.IOException: could not create audio stream from input stream'.
I heard somewhere else that some of the sun.audio classes im importing were having some problems. Could that be?
Thanks.
Miguel André.
I guess you are trying to play an mp3 file. Java doesnt support mp3 natively. Your code is capable of playing wave(*.wav) files only. JavaFX supports mp3 out-of-the-box. Java supports MP3 with use of an external plug-in(JMF,FMJ,JLayer ..)

Why getClass() used in the piece of code?

I am trying to access the audio file from the jar,for doing that i wrote a code,like
...
try {
InputStream is =
getClass().getResourceAsStream("audio.wav");
Player player = Manager.createPlayer(is, "audio/X-wav");
p.start();
}
catch(IOException ioe) {
}
catch(MediaException me) {
}
...
but here in this snippet,what is the use of getClass()?
It allows you to get the class of the current object, so in turn you can access resources. getResourceAsStream loads resources from the class loader associated with the class. See the documentation for Object.getClass and Class.getResourceAsStream.

Resources