Android play video while downloading inputstream mediaplayer
If your application does not fulfill those expectations, the user may have a bad experience. Read Building an Audio App for the full details. This section describes special instructions for managing a MediaPlayer when it is implemented inside a service. First of all, like an Activity , all work in a Service is done in a single thread by default—in fact, if you're running an activity and a service from the same application, they use the same thread the "main thread" by default.
Therefore, services need to process incoming intents quickly and never perform lengthy computations when responding to them. If any heavy work or blocking calls are expected, you must do those tasks asynchronously: either from another thread you implement yourself, or using the framework's many facilities for asynchronous processing.
For instance, when using a MediaPlayer from your main thread, you should call prepareAsync rather than prepare , and implement a MediaPlayer. OnPreparedListener in order to be notified when the preparation is complete and you can start playing. For example:. On synchronous operations, errors would normally be signaled with an exception or an error code, but whenever you use asynchronous resources, you should make sure your application is notified of errors appropriately.
In the case of a MediaPlayer , you can accomplish this by implementing a MediaPlayer. OnErrorListener and setting it in your MediaPlayer instance:.
It's important to remember that when an error occurs, the MediaPlayer moves to the Error state see the documentation for the MediaPlayer class for the full state diagram and you must reset it before you can use it again.
Using wake locks When designing applications that play media in the background, the device may go to sleep while your service is running. Because the Android system tries to conserve battery while the device is sleeping, the system tries to shut off any of the phone's features that are not necessary, including the CPU and the WiFi hardware. However, if your service is playing or streaming music, you want to prevent the system from interfering with your playback. In order to ensure that your service continues to run under those conditions, you have to use "wake locks.
Notice: You should always use wake locks sparingly and hold them only for as long as truly necessary, because they significantly reduce the battery life of the device. Once you do, the MediaPlayer holds the specified lock while playing and releases the lock when paused or stopped:. However, the wake lock acquired in this example guarantees only that the CPU remains awake.
If you are streaming media over the network and you are using Wi-Fi, you probably want to hold a WifiLock as well, which you must acquire and release manually. When you pause or stop your media, or when you no longer need the network, you should release the lock:. As mentioned earlier, a MediaPlayer object can consume a significant amount of system resources, so you should keep it only for as long as you need and call release when you are done with it.
It's important to call this cleanup method explicitly rather than rely on system garbage collection because it might take some time before the garbage collector reclaims the MediaPlayer , as it's only sensitive to memory needs and not to shortage of other media-related resources. So, in the case when you're using a service, you should always override the onDestroy method to make sure you are releasing the MediaPlayer :.
You should always look for other opportunities to release your MediaPlayer as well, apart from releasing it when being shut down. For example, if you expect not to be able to play media for an extended period of time after losing audio focus, for example , you should definitely release your existing MediaPlayer and create it again later.
On the other hand, if you only expect to stop playback for a very short time, you should probably hold on to your MediaPlayer to avoid the overhead of creating and preparing it again. Starting with Android 8. They are similar to the low-level API provided by MediaDrm , but they operate at a higher level and do not expose the underlying extractor, drm, and crypto objects.
The current implementation can handle the following content types:. The following code snippet demonstrates how to use the new DRM MediaPlayer methods in a simple synchronous implementation. To manage DRM-controlled media, you need to include the new methods alongside the usual flow of MediaPlayer calls, as shown below:.
Start by initializing the MediaPlayer object and setting its source using setDataSource , as usual. Then, to use DRM, perform these steps:. If MediaPlayer. DrmInfo exists:. By default, prepareDrm runs synchronously, blocking until preparation is finished. However, the very first DRM preparation on a new device may also require provisioning, which is handled internally by prepareDrm , and may take some time to finish due to the network operation involved.
You can avoid blocking on prepareDrm by defining and setting a MediaPlayer. When you set an OnDrmPreparedListener , prepareDrm performs the provisioning if needed and preparation in the background. When provisioning and preparation have finished, the listener is called. You should not make any assumption about the calling sequence or the thread in which the listener runs unless the listener is registered with a handler thread. The listener can be called before or after prepareDrm returns. OnDrmPreparedListener to start the player.
They work in conjunction with prepareAsync , as shown below:. Another feature that may be useful in a media player application is the ability to retrieve music that the user has on the device.
You can do that by querying the ContentResolver for external media:. To use this with the MediaPlayer , you can do this:. Content and code samples on this page are subject to the licenses described in the Content License.
App Basics. Build your first app. App resources. Resource types. App manifest file. Device compatibility. Multiple APK support. Tablets, large screens, and foldables. Build responsive UIs. Build for foldables. Getting started. Handling data. User input. Watch Face Studio. Health services. Creating watch faces.
Android TV. Build TV Apps. Build TV playback apps. Help users find content on TV. Recommend TV content. Watch Next. Build TV games. Build TV input services. TV Accessibility. Android for Cars. Build media apps for cars. Build navigation, parking, and charging apps for cars. Android Things. Supported hardware. Advanced setup. Build apps. Create a Things app.
Communicate with wireless devices. Configure devices. Interact with peripherals. Build user-space drivers. Manage devices. Create a build. Push an update. Chrome OS devices. Show 5 more comments. Active Oldest Votes. You can create local proxy that will save the stream. In the VideoView open the localhost url of your ServerSocket. You will need to handle buffering, synchronizing threads etc.
Milos Fec Milos Fec 5 5 silver badges 11 11 bronze badges. I want to have only one inputStream to save double data consumption. BTW i dont know how to create local proxy. An explanation will be appreciated. Here you have example of using ServerSocket: stackoverflow. MediaPlayer accepts only web url, fileDescripter, and local file uri to play a video. So how StreamingThread will play video? When you create ServerSocket you will have localhost url i. You can set your own port i. I did whatever u suggested.
Now i have a new problem that whenever i hit local socket server link an exception is thrown at server side. See this que stackoverflow. Add a comment. I have put the whole code as a library on github check here EDIT: This lib does not work with all the types of videos where video servers imposes restrictions over downloading by breaking video into chunks and so on I have tested it with mp4 only.
This lib requires a public link. Im using firebase — John dahat. Check this demo app that uses this lib github. Johndahat There are issues with my lib. I don't have enough time to fix them. I would suggest you have a look at this github. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar.
Before starting your application, Android studio will display following screens. By default you would see the pause button disabled. Now press play button and it would become disable and pause button become enable. Up till now, the music has been playing. Now press the pause button and see the pause notification. Now when you press the play button again, the song will not play from the beginning but from where it was paused. Now press the fast forward or backward button to jump the song forward or backward 5 seconds.
A time came when the song cannot be jump forward. Your music would remain playing in the background while you are doing other tasks in your mobile.