www read Android external music file

Source: Internet
Author: User
Tags windows 7 x64 unity 5

Demand analysis

Using the Everyplay (2121-1540 version) recording screen, after upgrading the SDK, encountered a problem, call Android native Mediaplay to play music, when the recording screen can not record to sound, so the solution is to play music files in unity.

Probably explain:

When you select a song on your phone (other music files on your phone), play directly in unity. It is important to note that the format, size, and storage location of this music is unknown.

Test environment

Windows 7 x64

Unity 5.3.6 F1 X64

Huawei Android 4.2.2 (not rooted)

Samsung NOTE3 Android 4.3 (not rooted)

Play other directory audio files (can be Chinese file name, example: Thousands of miles away. mp3)

Test code

The path to the music file in the following code, when testing, switch to your own path.

    • "/storage/emulated/0/music/thousands of miles away. mp3";
    • "/storage/emulated/0/music/Love You Long live." OGG;
    • "/storage/emulated/0/music/Fish Leong _ Ningxia. wav";
usingSystem;usingUnityengine;usingSystem.Collections;usingSystem.IO;/// <summary>///Detail: Using www to play audio files///Author:qingqing-zhao ([email protected])///createtime: #CreateTime #/// </summary> Public classplayaudiobywww:monobehaviour{PrivateAudiosource Curaudiosource;  PublicAudiosource Curaudiosource {Get        {            if(Curaudiosource! =NULL)returnCuraudiosource; Curaudiosource= gameobject.getcomponent<audiosource>(); if(Curaudiosource = =NULL) {Curaudiosource= gameobject.addcomponent<audiosource>(); Curaudiosource.playonawake=false; Curaudiosource.maxdistance=1.1f; }            returnCuraudiosource; }    }     Public voidOngui () {if(GUI. Button (NewRect ( -, $, Max, -),"Stop"))        {            if(Curaudiosource! =NULL) Curaudiosource.stop (); }        if(GUI. Button (NewRect ( -, $, Max, -),"Play-mp3"))        {            stringAudiopath ="/storage/emulated/0/music/thousands of miles away. mp3";        Playlocalfile (audiopath); }        if(GUI. Button (NewRect ( -, -, Max, -),"Play-ogg"))        {            stringAudiopath ="/storage/emulated/0/music/Love You Long live. OGG";        Playlocalfile (audiopath); }        if(GUI. Button (NewRect ( -, -, Max, -),"Play-wav"))        {            stringAudiopath ="/storage/emulated/0/music/Fish Leong _ Ningxia. wav";        Playlocalfile (audiopath); }    }    voidPlaylocalfile (stringaudiopath) {        varexists =file.exists (audiopath); Debug.logformat ("{0}, Existence: {1}", audiopath, exists); Startcoroutine (Loadaudio (audiopath, (AudioClip)={Curaudiosource.clip=AudioClip;        Curaudiosource.play ();    })); } IEnumerator Loadaudio (stringFilePath, action<audioclip>loadfinish) {        //file paths on Android and PCFilePath ="file:///"+FilePath; Debug.logformat ("local AudioClip: {0}", FilePath); www www=NewWWW (FilePath); yield returnwww; if(string. IsNullOrEmpty (Www.error)) {AudioClip AudioClip=NULL; //ogg file will report: Streaming of ' Ogg ' on this platform are not supported//if (Filepath.endswith ("Ogg"))//{            //AudioClip = Www.GetAudioClip (False, True, Audiotype.oggvorbis); //}            //Else{AudioClip=Www.audioClip;        } loadfinish (AudioClip); }        Else{Debug.logerrorformat ("www load file error:{0}", Www.error); }    }}

code file: Https://github.com/zhaoqingqing/blog_samplecode/blob/master/technical-research/PlayAudioByWWW.cs

Summary of my

We know that when using www loading assetbundle, it is not possible to have Chinese name and Chinese path , but the method used above, why the Chinese file can be loaded and played properly?

The above is simply loading the file via WWW, and the loaded file is not in Unity's assetbundle format, so the notation is not the same as loading assetbundle.

Problems that exist

When the Ogg format music file is loaded via WWW, it will be reported "streaming of ' Ogg ' on this platform are not supported"

As for the resolution of this error, my solution now is: In the selection of music, if it is the Ogg format file pop-up hint.

I tried to use audioclip AudioClip = Www.GetAudioClip (False, True, Audiotype.oggvorbis); or get the same error message.

www read Android external music file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.