ExoPlayer - how to play local mp3 file - android

I'm trying to use ExoPlayer instead of MediaPlayer because it's a common bug that MediaPlayer returns wrong getCurrentPosition() and I need a substitute.
But I can't find an info anywhere how to open a local file through the file path to the file same as MediaPlayer's .setDataSource(String filepath)
Google doesn't have any example and the official documentation site strangely crash my FireFox browser on both computers

The ExoPlayer demo app in github can be modified to play local files.
To do that, edit https://github.com/google/ExoPlayer/blob/master/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java file to add a new video set.
public static final Sample[] LOCAL_VIDEOS = new Sample[] {
new Sample("Some User friendly name of video 1",
"/mnt/sdcard/video1.mp4", DemoUtil.TYPE_OTHER),
new Sample("Some User friendly name of video 2",
"/mnt/sdcard/video2.mp4", DemoUtil.TYPE_OTHER),
};
To do that, edit https://github.com/google/ExoPlayer/blob/master/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java file to add a new sample set.
sampleAdapter.add(new Header("Local Videos"));
sampleAdapter.addAll((Object[]) Samples.LOCAL_VIDEOS);

A minor modification with Srikanth Peddibhotla's code works
The Uri string for the file should be "file:///mnt/sdcard/YourFilename.mp4" instead of "/mnt/sdcard/YourFilename.mp4" in Samples.java
public static final Sample[] LOCAL_VIDEOS = new Sample[] {
new Sample("Some User friendly name of video 1",
"file:///mnt/sdcard/video1.mp4", DemoUtil.TYPE_MP4),
new Sample("Some User friendly name of video 2",
"file:///mnt/sdcard/video2.mp4", DemoUtil.TYPE_MP4),
};
Also, add the following lines to SampleChooserActivity.java
sampleAdapter.add(new Header("Local Videos"));
sampleAdapter.addAll((Object[]) Samples.LOCAL_VIDEOS);

Using ExoPlayer 2.1, and starting with the demo project, you can play mp3 files from the assets folder without modifying any Java code, just by adding the mp3 files in the assets folder and creating or modifying a json file. Starting with the ExoPlayer demo project:
Put the mp3 files in the demo/assets folder (with media.exolist.json).
Either modify media.exolist.json or create a new file such as my.exolist.json containing one or more entries formatted like this:
{
"name": "Children's Songs",
"samples": [
{
"name": "Mary Had a Little Lamb",
"uri": "asset:///mary1.mp3"
},
{
"name": "Itsy Bitsy Spider",
"uri": "asset:///spider1.mp3"
}
]
},
(The final comma assumes there will be another category following, such as Blues Songs, Jazz Songs etc. with more mp3 entries. The final category has no comma after it.)
The figure below shows the chooser activity screen after you click on Children's Songs:
Click Mary Had a Little Lamb or Itsy Bitsy Spider and that mp3 plays.

Google changed some variable name and class definition these days! Below differ works for me.
--- a/demo/src/main/java/com/google/android/exoplayer/demo/SampleChooserActivity.java
+++ b/demo/src/main/java/com/google/android/exoplayer/demo/SampleChooserActivity.java
## -30,6 +28,8 ## import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
## -44,7 +44,12 ## public class SampleChooserActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_chooser_activity);
final List<SampleGroup> sampleGroups = new ArrayList<>();
- SampleGroup group = new SampleGroup("YouTube DASH");
+
+ SampleGroup group = new SampleGroup("test videos");
+ group.addAll(Samples.LOCAL_VIDEOS);
+ sampleGroups.add(group);
+
+ group = new SampleGroup("YouTube DASH");
group.addAll(Samples.YOUTUBE_DASH_MP4);
group.addAll(Samples.YOUTUBE_DASH_WEBM);
sampleGroups.add(group);
diff --git a/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java b/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
index 9f58528..9e86f99 100644
--- a/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
+++ b/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
## -248,6 +248,13 ## import java.util.Locale;
"http://vod.leasewebcdn.com/bbb.flv?ri=1024&rs=150&start=0", Util.TYPE_OTHER),
};
+ public static final Sample[] LOCAL_VIDEOS = new Sample[] {
+ new Sample("Some User friendly name of video 1",
+ "file:///mnt/sdcard/test1.mp4", Util.TYPE_OTHER),
+ new Sample("Some User friendly name of video 2",
+ "file:///mnt/sdcard/test2.mp4", Util.TYPE_OTHER),
+ };
+
private Samples() {}
}

Related

Play a youtube video inside an AIR app (AS3)

I'd like to know if it's possible to play a youtube video inside an AIR app using AS3 code.
It seems that youtube API is deprecated...
If anybody knows a way or a good tutorial.
Thx
----------------EDIT
I've tried this code :
var wt:uint;
var ht:uint ;
var webview:StageWebView ;
var rect:Rectangle;
var url:String;
url = "https://www.youtube.com/watch?v=hFupHx5G44s";
trace(url);
wt = this.stage.stageWidth;
ht = this.stage.stageHeight;
webview = new StageWebView();
rect = new Rectangle(0,300,wt,ht/2);
webview.stage = this.stage;
webview.viewPort = rect;
webview.loadURL(url);
But it's loading the all page of youtube (youtube video + the recommended videos..) with a scroller on the right.
I'd like to load only the video (not all the youtube page).
Edit :
I'd like to load only the video (not all the youtube page).
Use this link format :
url = "https://www.youtube.com/v/hFupHx5G44s";
for example, choose a format like below :
https://www.youtube.com/v/hFupHx5G44s - use the /v/ to get a SWF version
https://www.youtube.com/embed/hFupHx5G44s - use the /embed/ to get a HTML5 version
It's for an ANDROID AIR app.
You should have mentioned that detail in the question. You could try adding
Security.allowDomain("www.youtube.com");
Security.loadPolicyFile("https://www.youtube.com/crossdomain.xml");
//////////# End of Edit
Why not use stageWebView to load the embed (HTML5) player as web page?
The YouTube AS3 API being deprecated likely means you cannot create your own user interface or use features like search. You can re-create the search function easily though (load "search results" source into String and extract links by parsing the source code).
Anyways, I just tried this code below and it worked for an AIR Desktop App.Maybe you can use it as a starting point...
package
{
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.*;
import flash.system.*;
public class Youtube_AIR_code extends MovieClip
{
public var YT_Loader : Loader;
public var my_VIDEO_ID : String = "hFupHx5G44s"; //# the YouTube ID
public function Youtube_AIR_code ()
{
Security.loadPolicyFile("http://www.youtube.com/crossdomain.xml");
YT_Loader = new Loader();
YT_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, YT_Ready );
YT_Loader.load( new URLRequest("http://www.youtube.com/v/" + my_VIDEO_ID) );
}
function YT_Ready (e:Event) : void
{
trace("YouTube SWF :: [status] :: Loading Completed");
//addChild(YT_Loader);
//# Wait for Youtube Player to initialise
YT_Loader.content.addEventListener("onReady", on_YT_PlayerLoaded );
}
private function on_YT_PlayerLoaded (e:Event) : void
{
//# Check Original Width/Height - Scale it proportionally
trace( "YT_Loader content Width : " + YT_Loader.content.width );
trace( "YT_Loader content Height : " + YT_Loader.content.height );
//# Testing changed size and position
YT_Loader.width = 320; YT_Loader.height = 240;
YT_Loader.x = 30; YT_Loader.y = 100;
addChild(YT_Loader); //# can add to stage only
}
} //# End Public Class
} //# End Package
Have you seen the Video Player ANE from My Flash Labs?
http://www.myflashlabs.com/product/video-player-ane-adobe-air-native-extension/

How to convert any format(.txt, .Doc) file into epub file in android application

I am developing an application in which I have to read or write any eBook.
In android so many libraries available for reading any eBook, but for writing i didn't find any thing.
For reading any eBook file must be in .epub format.
I have an editor in which i am entering some text and after saving that file in any format how can i convert that file into .epub file.
Thanks in advance.
In android Java is developer language for create APP, use Epublib (lib in java)
Read "doc file" with poi
Create epub file:
package nl.siegmann.epublib.examples;
import java.io.InputStream;
import java.io.FileOutputStream;
import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Metadata;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.epub.EpubWriter;
public class Translator {
private static InputStream getResource( String path ) {
return Translator.class.getResourceAsStream( path );
}
private static Resource getResource( String path, String href ) {
return new Resource( getResource( path ), href );
}
public static void main(String[] args) {
try {
// Create new Book
Book book = new Book();
Metadata metadata = book.getMetadata();
// Set the title
metadata.addTitle("Epublib test book 1");
// Add an Author
metadata.addAuthor(new Author("Joe", "Tester"));
// Set cover image
book.setCoverImage(
getResource("/book1/test_cover.png", "cover.png") );
// Add Chapter 1
book.addSection("Introduction",
getResource("/book1/chapter1.html", "chapter1.html") );
// Add css file
book.getResources().add(
getResource("/book1/book1.css", "book1.css") );
// Add Chapter 2
TOCReference chapter2 = book.addSection( "Second Chapter",
getResource("/book1/chapter2.html", "chapter2.html") );
// Add image used by Chapter 2
book.getResources().add(
getResource("/book1/flowers_320x240.jpg", "flowers.jpg"));
// Add Chapter2, Section 1
book.addSection(chapter2, "Chapter 2, section 1",
getResource("/book1/chapter2_1.html", "chapter2_1.html"));
// Add Chapter 3
book.addSection("Conclusion",
getResource("/book1/chapter3.html", "chapter3.html"));
// Create EpubWriter
EpubWriter epubWriter = new EpubWriter();
// Write the Book as Epub
epubWriter.write(book, new FileOutputStream("test1_book1.epub"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

Download an MP3 from server and play multiple times (loop). Multiple downloads?

I have a simple button that plays a small MP3 file, looping it 30 times. The MP3 is streamed from a server (urlMP3).
I can see on my Galaxy S2 that it accesses the server for each of the 30 loops. Is it downloading the MP3 each time it loops or downloading once and playing from the phone's memory?
//Button 'audioYes' to play audio loop x 30
var soundLoop:Sound = new Sound();
var soundChannel:SoundChannel;
var soundLoopUrl:URLRequest = new URLRequest(urlMP3);
audioYes.addEventListener(MouseEvent.CLICK, f2_MouseClickHandler);
function f2_MouseClickHandler(event:MouseEvent):void {
soundLoop.load(soundLoopUrl);
soundLoop.play(0, 30);
}
If it is downloading each time, what would be a good way to download it once and then play? Thanks for your help.
Edit: Sep 1 2012
I've created a simple flash file and added the following provided by #Rytis. I'm getting an error from the last line, this.mySound.play "Error #1009: Cannot access a property or method of a null object reference." What do I do with that?
import flash.media.Sound;
import flash.net.URLLoader;
import flash.display.Loader;
var mySound:Sound = new Sound();
var myurlLoader:URLLoader = new URLLoader();
myurlLoader.addEventListener(Event.COMPLETE, this.onSoundLoadComplete)
myurlLoader.load(new URLRequest("01.mp3"))
function onSoundLoadComplete(event:Event):void{
this.mySound = URLLoader(event.target).data as Sound;
this.mySound.play(0,30);
}
Short answer - download sound and store it to variable before playing it.
Example:
package {
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class SoundLoadTest {
protected var sound : Sound;
public function SoundLoadTest () {
var urlLoader : URLLoader = new URLLoader();
urlLoader.addEventListener( Event.COMPLETE, this.onSoundLoadComplete )
urlLoader.load( new URLRequest( "path/to/sound.file" ) )
}
protected function onSoundLoadComplete ( event : Event) : void {
// save loaded sound to a class field
this.sound = URLLoader( event.target ).data as Sound;
// start playing sound
this.sound.play( 0, 30 );
}
}
}

AS3 Android Air, audio stream error

I have a Android Actionscript project that I am trying to incorporate sound in, but having a problem that I cannot figure out. I have the sound file in the same directory as my class file and my .xfl file. I am trying to start the music file as soon as the application starts via class reference, but am getting the same error everytime:
Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.
at playAudio()[C:\pathtoerror\playAudio.as:9]
at networkScores/frame1()[networkScores::frame1:7]
Here is my class file:
package
{
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
public class playAudio extends Sound
{
public var music:Sound = new Sound(new URLRequest("mathHomeSong.mp3"));
public var sc:SoundChannel;
public var num:int;
public function playAudio(controlNum:int)
{
num = controlNum;
if(num == 1)
{
sc = music.play();
}
else if(num == 2)
{
}
}
}
}
Here is my call from the timeline:
import playAudio;
var playMusic:playAudio = new playAudio(1);
Just figured this out, I had to put a link to the mp3 file in my server, instead of putting it in my directory.
Sorry about the confusion
I posted too soon. I just figured out my question, figures. Ok, so what I did not do is post this file on my server and with the URLRequest(""); I had to put a link to my file on my server, not linki
The answer to this question is that I forgot to add this music file to a server and then link it via hyper link. For example http://mysite.com/audio.file
Sorry about this
thanks

Android: Simple way to upload a picture to an image host

I d like to upload a picture to a host like: http://imagerz.com/ .
Any sample for this? I can do a simple POST request, but how I can add the image content to my POST request?
Here is a tutorial that tells you how to send a file via FTP to a server. File upload and download using Java
It shouldn't be very hard to "port" that code into android. (You may have to change some of the classes/methods as some of them may not be implemented in Android's lightweight VM).
There are also other image hosting services that should have an api that you could follow.
EDIT:
As you stated, you wanted to do this with a post request.
I found this great tutorial with the following code:
package com.commonsbook.chap9;
import java.io.File;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.MultipartPostMethod;
public class HttpMultiPartFileUpload {
private static String url =
"http://localhost:8080/HttpServerSideApp/ProcessFileUpload.jsp";
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
MultipartPostMethod mPost = new MultipartPostMethod(url);
client.setConnectionTimeout(8000);
// Send any XML file as the body of the POST request
File f1 = new File("students.xml");
File f2 = new File("academy.xml");
File f3 = new File("academyRules.xml");
System.out.println("File1 Length = " + f1.length());
System.out.println("File2 Length = " + f2.length());
System.out.println("File3 Length = " + f3.length());
mPost.addParameter(f1.getName(), f1);
mPost.addParameter(f2.getName(), f2);
mPost.addParameter(f3.getName(), f3);
int statusCode1 = client.executeMethod(mPost);
System.out.println("statusLine>>>" + mPost.getStatusLine());
mPost.releaseConnection();
}
}
Source: http://www.theserverside.com/news/1365153/HttpClient-and-FileUpload
The same issues about porting this code to android as I stated above apply.

Categories

Resources