|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "nsSound.h" |
|
8 #include "nsObjCExceptions.h" |
|
9 #include "nsNetUtil.h" |
|
10 #include "nsCOMPtr.h" |
|
11 #include "nsIURL.h" |
|
12 #include "nsString.h" |
|
13 |
|
14 #import <Cocoa/Cocoa.h> |
|
15 |
|
16 NS_IMPL_ISUPPORTS(nsSound, nsISound, nsIStreamLoaderObserver) |
|
17 |
|
18 nsSound::nsSound() |
|
19 { |
|
20 } |
|
21 |
|
22 nsSound::~nsSound() |
|
23 { |
|
24 } |
|
25 |
|
26 NS_IMETHODIMP |
|
27 nsSound::Beep() |
|
28 { |
|
29 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
30 |
|
31 NSBeep(); |
|
32 return NS_OK; |
|
33 |
|
34 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
35 } |
|
36 |
|
37 NS_IMETHODIMP |
|
38 nsSound::OnStreamComplete(nsIStreamLoader *aLoader, |
|
39 nsISupports *context, |
|
40 nsresult aStatus, |
|
41 uint32_t dataLen, |
|
42 const uint8_t *data) |
|
43 { |
|
44 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
45 |
|
46 NSData *value = [NSData dataWithBytes:data length:dataLen]; |
|
47 |
|
48 NSSound *sound = [[NSSound alloc] initWithData:value]; |
|
49 |
|
50 [sound play]; |
|
51 |
|
52 [sound autorelease]; |
|
53 |
|
54 return NS_OK; |
|
55 |
|
56 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
57 } |
|
58 |
|
59 NS_IMETHODIMP |
|
60 nsSound::Play(nsIURL *aURL) |
|
61 { |
|
62 nsCOMPtr<nsIURI> uri(do_QueryInterface(aURL)); |
|
63 nsCOMPtr<nsIStreamLoader> loader; |
|
64 return NS_NewStreamLoader(getter_AddRefs(loader), uri, this); |
|
65 } |
|
66 |
|
67 NS_IMETHODIMP |
|
68 nsSound::Init() |
|
69 { |
|
70 return NS_OK; |
|
71 } |
|
72 |
|
73 NS_IMETHODIMP |
|
74 nsSound::PlaySystemSound(const nsAString &aSoundAlias) |
|
75 { |
|
76 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
77 |
|
78 if (NS_IsMozAliasSound(aSoundAlias)) { |
|
79 NS_WARNING("nsISound::playSystemSound is called with \"_moz_\" events, they are obsolete, use nsISound::playEventSound instead"); |
|
80 // Mac doesn't have system sound settings for each user actions. |
|
81 return NS_OK; |
|
82 } |
|
83 |
|
84 NSString *name = [NSString stringWithCharacters:reinterpret_cast<const unichar*>(aSoundAlias.BeginReading()) |
|
85 length:aSoundAlias.Length()]; |
|
86 NSSound *sound = [NSSound soundNamed:name]; |
|
87 if (sound) { |
|
88 [sound stop]; |
|
89 [sound play]; |
|
90 } |
|
91 |
|
92 return NS_OK; |
|
93 |
|
94 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
95 } |
|
96 |
|
97 NS_IMETHODIMP |
|
98 nsSound::PlayEventSound(uint32_t aEventId) |
|
99 { |
|
100 // Mac doesn't have system sound settings for each user actions. |
|
101 return NS_OK; |
|
102 } |